import $http from '../../http/http.js'
import apiSetting from '../../http/apiSetting.js'
let dayjs = require('dayjs');
let app = getApp();

Page({
  data: {
    delBtnShow: true,
    type: 'add',
    gradeArray: ['无等级', '一等级', '二等级', '三等级', '四等级', '五等级'],
    gradeIndex: null,
    isPassedArray: ['不合格', '合格'],
    isPassedIndex: null,
    factorsArray: [],
    // factorsIndex: null,
    form: {
      parentId: '',
      grade: '',
      outPut: '',
      batchNumber: '',
      isPassed: '',
      columnData: [],
      columns: []
    },
    // 质检指标列表
    qualityList: []
  },

  onLoad(query) {
    this.setData({
      qualityList: []
    })
    const pages = getCurrentPages();
    let currPage = pages[pages.length - 1]; //当前页面
    let prevPage = pages[pages.length - 2]; //上一个页面
    this.data.list = prevPage.data.list
    this.data.formStepCode = prevPage.data.form.stepCode
    if (query.type == 'add') {
      this.setData({
        delBtnShow: false
      })
      dd.setNavigationBar({
        title: '新增质检结果'
      })
      this.mateDataPropertyQuery()
    }
    if (query.type == 'edit') {
      dd.setNavigationBar({
        title: '编辑质检结果'
      })
      this.setData({
        delBtnShow: true
      })
      this.qualityControlGradeQuery(query)
    }
    this.setData({
      type: query.type,
      'form.parentId': query.parentId
    })
  },

  // 等级
  gradePickerChange(e) {
    this.setData({
      gradeIndex: e.detail.value,
      'form.grade': e.detail.value
    });
  },

  // 是否合格
  isOnTestPickerChange(e) {
    this.setData({
      isPassedIndex: e.detail.value,
      'form.isPassed': e.detail.value
    });
  },

  // 指标名称
  factorsPickerChange(e) {
    let ind = e.target.dataset.index
    let value = e.detail.value
    let find = this.data.qualityList.findIndex(n => {
      return n.columns == this.data.factorsArray[value].propertyCode
    })
    if (find != -1) {
      dd.alert({ content: "指标不能重复选择" });
      return
    }
    this.data.qualityList[ind].columns = this.data.factorsArray[value].propertyCode
    this.data.qualityList[ind].columnIndex = value
    this.setData({
      qualityList: this.data.qualityList
    });
    this.updateForm()
  },

  onInputOutPut(e) {
    let value = e.detail.value
    this.data.form.outPut = value
  },

  onInputBatchNumber(e) {
    let value = e.detail.value
    this.data.form.batchNumber = value
  },

  onInputChange(e) {
    let ind = e.target.dataset.index
    let value = e.detail.value
    this.data.qualityList[ind].columnData = value
    this.updateForm()
  },

  qualityControlGradeQuery(query) {
    let that = this
    // const pages = getCurrentPages();
    // let currPage = pages[pages.length - 1]; //当前页面
    // let prevPage = pages[pages.length - 2]; //上一个页面
    let list = that.data.list
    let find = list.find(n => {
      return n.id == query.id
    })
    that.setData({
      form: find,
      gradeIndex: find.grade,
      isPassedIndex: find.isPassed
    })
    console.log(find)
    that.mateDataPropertyQuery()
  },

  mateDataPropertyQuery() {
    let that = this
    $http(apiSetting.mateDataPropertyQuery, {
      stepCode: that.data.formStepCode,
      dataType: "QualityControl", //定值 工艺 - TechnocsParam 质检指标 - QualityControl
    }).then((data) => {
      that.setData({
        factorsArray: data.result.propertyDTOList
      })
      if (that.data.type == 'edit') {
        let arr = []
        for (let i in that.data.form.dapList) {
          let ind = that.data.factorsArray.findIndex(n => {
            return n.propertyCode == that.data.form.dapList[i].code
          })
          let obj = {
            columns: that.data.form.dapList[i].code,
            columnData: that.data.form.dapList[i].value,
            columnIndex: ind
          }
          arr.push(obj)
        }
        that.setData({
          qualityList: arr
        })
        that.updateForm()
      }
    })
  },

  addList() {
    let obj = {
      columns: '',
      columnData: '',
    }
    this.data.qualityList.push(obj)
    this.setData({
      qualityList: this.data.qualityList
    })
    this.updateForm()
  },

  delList(e) {
    let ind = e.target.dataset.index
    this.data.qualityList.splice(ind, 1)
    this.setData({
      qualityList: this.data.qualityList
    })
    this.updateForm()
  },

  save() {
    console.log(this.data.form)
    let num = Number(this.data.form.outPut)
    let reg = /^[+]?(\d|[1-9]\d+)$/;
    let ars = reg.test(num);
    if (ars && num > 0) {
    } else {
      dd.alert({ content: '质检数量必须大于0的正整数' });
      return
    }
    if (this.data.delBtnShow) {
      this.update()
    } else {
      this.add()
    }
  },

  add() {
    let that = this;
    $http(apiSetting.qualityControlGradeAdd, {
      batchNumber: that.data.form.batchNumber, //	批次号
      columnData: that.data.form.columnData, //	检验指标
      columns: that.data.form.columns, //	检验指标
      grade: that.data.form.grade, //	质检等级
      isPassed: that.data.form.isPassed, //	是否合格
      outPut: that.data.form.outPut, //	检验数量
      parentId: that.data.form.parentId, //	质检结果ID
    }).then((data) => {
      if (data.code == 0) {
        dd.navigateBack({
          delta: 1
        })
      }
    })
  },

  update() {
    let that = this
    $http(apiSetting.qualityControlGradeUpdate, {
      batchNumber: that.data.form.batchNumber, //	批次号
      columnData: that.data.form.columnData, //	检验指标
      columns: that.data.form.columns, //	检验指标
      grade: that.data.form.grade, //	质检等级
      isPassed: that.data.form.isPassed, //	是否合格
      outPut: that.data.form.outPut, //	检验数量
      id: that.data.form.id,
      parentId: that.data.form.parentId, //	质检结果ID
    }).then((data) => {
      if (data.code == 0) {
        dd.navigateBack({
          delta: 1
        })
      }
    })
  },

  // 删除
  del() {
    let that = this
    dd.confirm({
      title: '温馨提示',
      content: '删除不可恢复，你确定要删除？',
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      success: (result) => {
        if (result.confirm == true) {
          $http(apiSetting.qualityControlGradeDelete, {
            id: that.data.form.id
          }).then((data) => {
            if (data.code == 0) {
              dd.navigateBack({
                delta: 1
              })
            }
          })
        }
      },
    });
  },

  updateForm() {
    let columns = []
    let columnData = []
    for (let i in this.data.qualityList) {
      columns.push(this.data.qualityList[i].columns)
      columnData.push(this.data.qualityList[i].columnData)
    }
    this.data.form.columns = columns
    this.data.form.columnData = columnData
  }
});
